package es.ats.codington.festival.daos;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.Place;
import es.ats.codington.festival.exceptions.DAOSExceptions;
import es.ats.codington.festival.interfaces.daos.iPlaceDAOImpl;
import es.ats.codington.festival.services.PlaceService;
import es.ats.codington.festival.utils.FERSDataConnection.FersDC;
import es.ats.codington.festival.utils.Query.JSonQuery;

public class PlaceDAOImpl implements iPlaceDAOImpl {
	
	private Connection connection;
	private PreparedStatement statement;
	private ResultSet resultSet;
	//JSonQuery json = new JSonQuery();
	
	
	
	
	public PlaceDAOImpl(Connection connection) {
		super();
		this.connection = connection;
			}


	@Override
	public Place viewPlace(int idplace) throws Exception {

		Place place = new Place();
		try{
			//Setting commit is mandatory
			connection.setAutoCommit(false);
			
							
			// Now making changes			
			PreparedStatement myPlace = connection.prepareStatement("SELECT * FROM place WHERE idplace= ? ");
			myPlace.setInt(1, idplace);
			
		
			ResultSet thePlace = myPlace.executeQuery();
			
			while(thePlace.next()){
				place.setName(thePlace.getString("name"));
				place.setAddress(thePlace.getString("address"));
				place.setCapacity(thePlace.getInt("capacity"));
				place.setTown(thePlace.getString("town"));
				place.setDescription(thePlace.getString("description"));
				place.setType(thePlace.getString("type"));
				place.setiDplace(idplace);
							
			}
			
			connection.commit();
		
		}catch (Exception ex){
			System.out.println("Error Exception con mensaje: " + ex.getMessage());
			connection.rollback();
			
		}
	
		
		return place;
	}
	
	
	@Override
	public ArrayList<Place> viewPlaces() throws Exception {

		
		ArrayList<Place> placesList = new ArrayList<Place>();
		
		try{
			connection = FersDC.getConnection();
			if (connection == null) throw new IOException("It does not connect to Database");
			
			//Setting commit is mandatory
			connection.setAutoCommit(false);		
				
			// Now getting the table			
			PreparedStatement myViewPlaces = connection.prepareStatement(" SELECT * FROM place");
			ResultSet MyViews = myViewPlaces.executeQuery();
			
		
			while(MyViews.next()){
				Place place = new Place();
				
				place.setName(resultSet.getString("name"));
				place.setAddress(resultSet.getString("address"));
				place.setDescription(resultSet.getString("descriptions"));
				place.setTown(resultSet.getString("town"));
				place.setCapacity(resultSet.getInt("capacity"));
				place.setType(resultSet.getString("type"));
							
				placesList.add(place);
			}
			MyViews.close();
			connection.commit();
				
		}catch (Exception ex){
			System.out.println("Error Exception con mensaje: " + ex.getMessage());
			connection.rollback();
		}
		return placesList;
		
		
		
		
	}



	@Override
	public ArrayList<Integer> getIdsPlace(String type) throws Exception {

		ArrayList<Integer> idsPlaceByType= new ArrayList<Integer>();
		try{
			
							
			// Now making changes			
			PreparedStatement myPlace = connection.prepareStatement("SELECT idplace FROM place WHERE type= ? ");
			myPlace.setString(1, type);
			
			ResultSet ids = myPlace.executeQuery();  
			
			int iter=0;
			while (ids.next()) {
				idsPlaceByType.add(ids.getInt("idplace"));
				iter++;						
				}
	
					
		ids.close();
		return idsPlaceByType;
			
		
		}catch (Exception ex){
			System.out.println("Error Exception con mensaje: " + ex.getMessage());
			connection.rollback();
			
		}
		return idsPlaceByType;
		
		
	}


}
